import 'package:app/common/app_theme.dart';
import 'package:app/common/colors.dart';
import 'package:app/pages/bottom_tab_controller.dart';
import 'package:app/pages/login_modules/login_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/utils/persisten_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:get/route_manager.dart';
class VideoSplashScreenPage extends StatefulWidget {
  const VideoSplashScreenPage({Key? key}) : super(key: key);

  @override
  State<VideoSplashScreenPage> createState() => _VideoSplashScreenPageState();
}

class _VideoSplashScreenPageState extends State<VideoSplashScreenPage>with SingleTickerProviderStateMixin {

  late VideoPlayerController _controller;

  late AnimationController animationController;
  @override
  void initState() {
    super.initState();
    animationController = AnimationController(vsync: this,
      duration: const Duration(seconds: 10),
    );

    animationController.forward();
    animationController.addListener(() {
      setState(() {

      });
    });


    _controller = VideoPlayerController.asset('images/video_splash.mp4')
      ..initialize().then((_) {

        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });

    _controller.addListener(() {

      var curPosition = _controller.value.position;
      var totalPosition = _controller.value.duration;
      var curPos = curPosition.toString().substring(6, 7);
      var totalPos = totalPosition.toString().substring(6, 7);
      print("当前位置${curPos}，全部${totalPos} bilie:${int.parse(curPos)/int.parse(totalPos)}");
      if(_controller.value.isPlaying==false){

        Get.off(LoginView());
      }
    });

    _controller.play();

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child:Stack(
        // alignment:Alignment.center,
        fit: StackFit.expand,
        children: [
          Container(
            child: _controller.value.isInitialized
                ? AspectRatio(
              aspectRatio: 1/2,
              child: VideoPlayer(_controller),
            )
                : Container(),
          ),
          Align(
            alignment: Alignment.topRight,
            child: Container(
              margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+10,right: 20),
              child: SizedBox(
                height: 32,
                width: 32,
                child: Stack(
                  children: [
                    CircularProgressIndicator(
                      backgroundColor: Colors.grey[200],
                      valueColor:
                      ColorTween(begin: Colors.grey,end: Colors.grey[100]).animate(animationController),
                      value: animationController.value,
                    ),
                   Container(
                     height: 48,
                     width: 48,
                     color: Colors.transparent,
                     child:  GestureDetector(
                       onTap: (){
                         Get.offAllNamed(AppRoutes.lgoin);
                         animationController.stop();
                         _controller.pause();
                       },
                       child:  Center(
                         child: Text('跳过',style: appThemeData.textTheme.subtitle1!.copyWith(fontSize: 11),),
                       ),
                     ),
                   )
                  ],
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
